//
//  Const.swift
//  TuneMate
//
//  Created by 丸子 on 2023/11/22.
//

import UIKit

@_exported import SDWebImage
@_exported import SwiftyJSON
@_exported import HandyJSON
@_exported import SwifterSwift
@_exported import RxSwift
@_exported import RxRelay
@_exported import RxCocoa
@_exported import SnapKit
@_exported import DZNEmptyDataSet
@_exported import WCDBSwift


let decrypt_key = "Hj1daZBLU0UgxV3f"


public let SCREEN_WIDTH = UIScreen.main.bounds.size.width
public let SCREEN_HEIGHT = UIScreen.main.bounds.size.height
let kEdgeRect = CGRect(x: 0, y: kBarHeight, width: SCREEN_WIDTH, height: (SCREEN_HEIGHT - kBarHeight))
let kBarHeight: CGFloat = navHig()

let kSafeHeight: CGFloat = kSafeBottom()
let kTabbarHeight: CGFloat = 49.0
let kMusicBarHeight: CGFloat = kSafeHeight + kTabbarHeight;
let kStatuBarHeight: CGFloat = kStatusBarHeight()
let kBarRemainHeight: CGFloat = 44.0

//获取当前设备版本
public let CURRENT_VERSION = UIDevice.current.systemVersion
let kKeyWindow = UIApplication.shared.keyWindow

var bottomSafeAreaHeight: CGFloat {
    isiPhoneX() ? 34: 0
}
public func isiPhoneX() -> Bool
{
    if UIDevice.current.userInterfaceIdiom == .pad {
        return false
    }
    let size = UIScreen.main.bounds.size
    let notchValue: Int = Int(size.width/size.height * 100)
    if 216 == notchValue || 46 == notchValue {
        return true
    }
    return false
}
/**< 底部安全距离 */
fileprivate func kSafeBottom() -> CGFloat {
    if #available(iOS 11, *) {
        return UIApplication.shared.delegate?.window??.safeAreaInsets.bottom ?? 0
    } else {
        return 0
    }
}

func statusBarHeight() -> CGFloat {
    UIApplication.shared.statusBarFrame.height
}
public func navHig() -> CGFloat {
    return statusBarHeight() + 44
}

func formatSize(size: UInt64) -> String {
    let formatter = ByteCountFormatter()
    formatter.allowedUnits = [.useKB, .useMB, .useGB]
    formatter.countStyle = .file
    let sizeString = formatter.string(fromByteCount: Int64(size))
    return sizeString
}


func getFormatTimeWithTimeInterval(_ timeInterval:TimeInterval) -> String {
    if timeInterval.isNaN{
        return "00:00"
    }
    var Min = Int(timeInterval / 60)
    let Sec = Int(timeInterval.truncatingRemainder(dividingBy: 60))
    return String(format: "%02zd:%02zd", Min,Sec);
}

func isValidEmail(_ email: String) -> Bool {
    let regex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
    let emailPredicate = NSPredicate(format:"SELF MATCHES %@", regex)
    return emailPredicate.evaluate(with: email)
}

func getTimestamp() -> String {
    let date = Date()
    let timestamp = date.timeIntervalSince1970
    return String("\(Int(timestamp))")
}

func kHideKeyboard() {
    (UIApplication.shared.delegate?.window)??.endEditing(true)
}
/**< 异步  */
func wk_dispatch_async_on_global_queue(callback: @escaping () -> Void) {
    DispatchQueue.global().async(execute: callback)
}

/**< 主线程  */
func wk_dispatch_after_main_queue(delay: Double, callback: @escaping () -> Void) {
    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + delay, execute: callback)
}

func getTotalTime(totalTime:TimeInterval) -> String{
    let hours = Int(totalTime / 3600)
    let minutes = Int((totalTime.truncatingRemainder(dividingBy: 3600)) / 60)
    let seconds = Int(totalTime.truncatingRemainder(dividingBy: 60))
    
    let total: String
    if hours > 0 {
        total = String(format: "%02d:%02d:%02d", hours, minutes, seconds)
    } else {
        total = String(format: "%02d:%02d", minutes, seconds)
    }
    return total
}

//按比例获取宽度
public func GET_WIDTH(W: CGFloat) -> CGFloat {
    return W * SCREEN_WIDTH / 375.0
}
/**< 状态栏高度 */
fileprivate func kStatusBarHeight() -> CGFloat {
    var statusBarSize: CGSize?
    if #available(iOS 13.0, *) {
        statusBarSize = UIApplication.shared.windows.first?.windowScene?.statusBarManager?.statusBarFrame.size
    } else {
        statusBarSize = UIApplication.shared.statusBarFrame.size
    }
    var height: CGFloat = kJudgeIPhoneX() ? 47 : 20
    if let statusBarSize = statusBarSize, statusBarSize.height != 0 {
        height = min(statusBarSize.width, statusBarSize.height)
    }
    return height
}
/**< iPhoneX */
fileprivate func kJudgeIPhoneX() -> Bool {
    if #available(iOS 11, *) {
        return UIApplication.shared.delegate?.window??.safeAreaInsets.bottom ?? 0 > 0
    } else {
        return false
    }
}

func trimmingAllWhitespace(str:String) -> String {
    return str.components(separatedBy: .whitespacesAndNewlines).joined()
}
